window.onload = function(){
    let i;
    const btn = document.querySelector(".s_ck_all");
    const buttons = document.querySelectorAll(".s_ck");
    const del = document.querySelector(".del-all");
    const totalPrice = document.getElementById("totalPrice");
    const totalCount = document.getElementById("totalCount");
    const carBody = document.getElementById("carBody");
    const tr = carBody.getElementsByTagName("tr");
    const clear = document.querySelector(".clear");

    //第一步 设置全选
    btn.onclick = function(){
        if(btn.checked)
        {
            for(let i in buttons)
            {
                buttons[i].checked = true;
            }
        }
        else{
            for(let i in buttons)
            {
                buttons[i].checked = false;
            }
        }
        getTotal()
    };

    //第二步 设置选中删除
    del.onclick = function(){
        let tr = document.querySelectorAll("tr");
        //每次点击时更新tr的的长度，以判断是否全部删除
        for(i in buttons)
        {
            if(tr.length === 1)
            {
                alert("已全部删除");
                return;
            }
            else{
                if(buttons[i].checked === true)
                {
                    console.log( buttons[i])
                    buttons[i].parentNode.parentNode.remove();
                    getTotal()
                }
            }
        }
    };


//数量加减、删除
    for(i = 0; i<tr.length; i++){
        tr[i].onclick = function(e){
            var e = window.event || e;
            var target = e.target || e.srcElement;
            var input1 = this.getElementsByTagName("input")[1];

            switch(target.className){
                case 'add':
                    input1.value = parseInt(input1.value) + 1;
                    break;
                case 'reduce':
                    if(input1.value > 1){
                        input1.value = parseInt(input1.value) - 1;
                    }else{
                        input1.value = 1;
                    }
                    break;
                case 'del':
                    carBody.removeChild(target.parentNode.parentNode)
                    break;
            }
            getTotal()
        }
    }



//合计、已选商品件数
    function getTotal(){
        var carBody = document.getElementById("carBody");
        var tr = carBody.getElementsByTagName("tr");
        var price = 0;
        var count = 0;

        for(var i=0;i<tr.length;i++){
            var input = tr[i].getElementsByTagName("input");
            if(input[0].checked){
                var td2 = tr[i].getElementsByTagName('td')[2].innerText;
                var numCount = parseInt(input[1].value);
                tr[i].getElementsByTagName('td')[4].innerText = td2 * numCount;

                price += parseFloat(tr[i].getElementsByTagName('td')[4].innerText)
                count += parseInt(input[1].value)
            }
        }

        totalPrice.innerText = price.toFixed(2);
        totalCount.innerText = count;
    }

//清空购物车
    clear.onclick = function(){
        const parent = carBody.parentNode;
        parent.removeChild(carBody);
        totalPrice.innerText = 0;
        totalCount.innerText = 0;
    }



};